package org.martin.snsrs.actions;

import org.martin.snsrs.beans.User;
import org.martin.snsrs.daos.UserDao;
import org.martin.snsrs.services.UserServices;
import org.martin.snsrs.utils.MathUtil;

import com.opensymphony.xwork2.ActionSupport;

public class UserLogin extends UserBase{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4646265394178022558L;

	private String username;
	
	private String password;
	
	private String sessionKey;
	
	private UserDao userDao;
	
	private boolean isValidate;
	
	private String nickname;
	
	private Integer userID;
	
	private UserServices userServices;
	
	public String execute() throws Exception{
		if(!isValidate)
			return ERROR;
		User user = userDao.getUserByUsername(username);
		if(user != null && user.getUserPass().equals(MathUtil.getMD5(password))){
			sessionKey = userServices.generateSessionKey(username);
			userID = user.getUserID();
			nickname = user.getNickname();
			username = user.getUsername();
		}
		else {
			status = "error";
			errorReason = "用户名/密码错误";
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserServices getUserServices() {
		return userServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public void validate(){
		isValidate = true;
		if(username == null){
			isValidate = false;
			status = "error";
			errorReason = "用户名为空";
		}
		if(password == null){
			isValidate = false;
			status = "error";
			errorReason = "密码为空";
		}
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public Integer getUserID() {
		return userID;
	}

	public void setUserID(Integer userID) {
		this.userID = userID;
	}
}
